package durak.common.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
//import java.util.List;

//import durak.server.control.Card;

public class Cards implements Iterable<Card>, Serializable {

	/**
     * 
     */
	private static final long serialVersionUID = -4616417229962485079L;
	protected List<Card> cards;

	/**
	 * 
	 */
	public Cards() {
		cards = new ArrayList<Card>();
	}

	/**
	 * @param cards
	 */
	public Cards(List<Card> cards) {
		this.cards = new ArrayList<Card>(cards);
	}

	public Cards(Card card) {
		cards = new ArrayList<Card>();
		cards.add(card);
	}

	/**
	 * @return the cards
	 */
	public List<Card> getCards() {
		return cards;
	}

	/**
	 * @param list
	 *            the cards to set
	 */
	public void setCards(List<Card> list) {
		this.cards = list;
	}

	/**
	 * Returns number of cards in the deck
	 * 
	 * @return
	 */
	public int size() {
		return cards.size();
	}

	public boolean isEmpty() { // to check if the deck is empty or not
		if (cards == null)
			return true;
		return cards.isEmpty();
	}

	/**
	 * Returns first card from the deck, but doesn't remove it from the deck
	 * 
	 * @return
	 */

	public Card getFirstCard() {
		if (cards.size() > 0)
			return (cards.get(0));
		else
			return null;
	}

	public void shuffle() // Shuffle the giving deck
	{
		Collections.shuffle(cards, new Random());
	}

	public void addCard(Card card) {
		cards.add(card);
	}

	public Card DrawFirstCard() {
		if (cards.size() > 0)
			return (cards.remove(0));
		else
			return null;
	}

	public Card DrawCardByLocation(int i) {
		if (cards.size() > 0)
			return (cards.remove(i));
		else
			return null;
	}

	public void removeCard(Card card) // updating the hand with removed card
	{
		cards.remove(card);
	}

	public void removeCards(Cards cardsToRemove) {
		for (Card removeCard : cardsToRemove) {
			cards.remove(removeCard);
		}
	}

	public void removeAll() {
		this.cards.clear();
	}

	public Card getCardByLocation(int i) // updating the hand with removed card
	{

		return cards.get(i);
	}

	public String toString() {
		String returnString = new String();
		for (Card card : cards)
			returnString +=  " " + card.toString();
		return returnString;
	}

	public Card getLastCard() {

		System.out.println("in getLastCard");
		System.out.println(cards.size() + " cards");
		// System.out.println(cards.get(cards.size() - 1));
		if (cards.size() == 0)
			return null;
		return cards.get(cards.size() - 1);
	}

	@Override
	public Iterator<Card> iterator() {
		return cards.iterator();
	}

	public void addCards(Cards cards) {
		for (Card card : cards) {
			addCard(card);
		}
	}

	public boolean hasCard(Card cardFromHand) {
		return cards.contains(cardFromHand);
	}

}
